/**
 * Created by Smallbug on 2017-03-02.
 */
$(function() {
	init();
});

/* 页面初始化 */
function init() {
	$(".js-click-post").click(handlerOfClickPost);
	$(".js-click-put").click(handlerOfClickPut);
	$(".js-click-delete").click(handlerOfClickDelete);
	$(".js-click-get").click(handlerOfClickGet);

    $("#uploadFile").on("change", uploadChatFile);
}

/* 上传文件 需要在yml文件中设置文件上传大小限制 */
function uploadChatFile() {
    $("#upload-file-message").removeClass('css-message-warining');
    var formData = new FormData();
    formData.append("uploadfile",document.getElementById("uploadFile").files[0]);
    $.ajax({
        url : "user/upload",
        type : "POST",
        data : formData,
        processData : false,
        contentType : false,
        cache : false,
        success : function(data) {
            // Handle upload success
            if(data==true){
                $("#upload-file-message").text("分析成功！"+"您上传的图片经检验为真");
            }else{
                $("#upload-file-message").text("分析成功！"+"您上传的图片经检验为假").addClass("css-message-warining");
            }
        },
        error : function() {
            // Handle upload error
            $("#upload-file-message").text("上传失败");
        }
    });
    $("#upload-file-message").text("图片已上传，分析中...");
}

function handlerOfClickPost() {
	var user = {};
	user.id = 2;
	user.userName = "weijf";
	user.userPass = "weijf1234";

	$.ajax({
		url : "user/add",
		type : "post",
		data :user,
		dataType : 'json',
		success : function(data) {
			if (data != null) {
				alert(data);
			} else {
				alert("23333");
			}
		}
	});
}

function handlerOfClickPut() {
	var user = {};
	user.id = 42;
    user.userName = "weijf";
    user.userPass = "weijf1234";

	$.ajax({
		url : "user/10",
		type : "put",
		data :user,
		dataType : 'json',
		success : function(data) {
			if (data != null) {
				alert(data);
			} else {
				alert("23333");
			}
		}
	});
}

function handlerOfClickDelete() {
	$.ajax({
		url : "user/12",
		type : "delete",
		dataType : 'json',
		success : function(data) {
			if (data != null) {
				alert(data);
			} else {
				alert("23333");
			}
		}
	});

}

function handlerOfClickGet() {
	
	$.ajax({
		url : "user/233",
		type : "get",
		dataType : 'json',
		success : function(data) {
			if (data != null) {
				alert(data);
			} else {
				alert("23333");
			}
		}
	});

}